package com.briup.ch08;

import com.briup.ch07.Stu;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/**
 * @Author briup-adam
 * @Date 2023/10/13 下午3:00
 * @Description set集合 无序且不可重复
 * hashSet 可以存放null  但是只能有一个
 **/

public class SetBasic {
    public static void main(String[] args) {
        Set set = new HashSet();
        set.add("马俊辉");
        set.add("马家驹");
        set.add("张锦");
        set.add("海鹏");
        set.add("张锦");
        set.add(null);
        set.add(null);
        System.out.println(set.size());//4
        System.out.println(set);

        set.clear();
        Student stu5 = new Student("张美娜", 12);
        Student stu1 = new Student("张锦", 15);
        Student stu2 = new Student("马俊辉", 13);
        Student stu3 = new Student("海鹏", 14);
        Student stu4 = new Student("马家驹", 16);

        set.add(stu3);
        set.add(stu2);
        set.add(stu1);
        set.add(stu4);
        set.add(stu5);


        System.out.println(set.size());//1
        System.out.println(set);
//比较器排序 优先于自然排序
//        自然排序是让元素自身具备比较性
        TreeSet set1 = new TreeSet();
        set1.add(7);
        set1.add(27);
        set1.add(3);
        set1.add(31);
        set1.add(5);
        System.out.println(set1);
        set1.clear();
        set1.add("tom");
        set1.add("andy");
        set1.add("robin");
        set1.add("larry");
        set1.add("terry");
        System.out.println(set1);
        set1.clear();
        set1.addAll(set);
        System.out.println(set1);
        Comparator<Student> cmp=new ComparatorImpl();
        cmp=new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge()%2==0?-1:1;
            }
        };
        //函数式编程
        cmp=( s1, s2)->s1.getAge()%2==0?1:-4;

        TreeSet set2=new TreeSet(cmp);
        set2.addAll(set1);
        System.out.println(set2);

    }
}
class ComparatorImpl  implements  Comparator<Student>{

    @Override
    //o1 待排  o2 已排
    public int compare(Student o1, Student o2) {
        return o1.getAge()%2==0?-1:1;
    }
}
